package com.example.park2212.visitor

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.blankj.utilcode.util.ToastUtils
import com.example.lib_base.entity.VisitStatusBean
import com.example.lib_base.mvvm.BaseActivity
import com.example.park2212.R
import com.example.park2212.adapter.VisitAdapter
import com.example.park2212.adapter.VisitStatusAdapter
import com.example.park2212.databinding.ActivityVisitorBinding
import dagger.hilt.android.AndroidEntryPoint

@AndroidEntryPoint
@Route(path = "/app/visitor/VisitorActivity")
class VisitorActivity : BaseActivity<ActivityVisitorBinding,VisitorViewModel>() {
    val visitStatusAdapter by lazy { VisitStatusAdapter() }
    val visitAdapter by lazy { VisitAdapter() }
    override fun initView() {

        binding.headVisit.headerAdd.setOnClickListener {
            ARouter.getInstance().build("/app/visitor/VisitAddActivity").navigation()
        }

//        visitAdapter.setOnItemClickListener { adapter, view, position ->
//            ARouter.getInstance().build("/app/visitor/")
//        }

        val list = arrayListOf<VisitStatusBean>(
            VisitStatusBean("全部状态",true),
            VisitStatusBean("待审核",false),
            VisitStatusBean("已通过",false),
            VisitStatusBean("驳回",false),
            VisitStatusBean("已完成",false)
        )
        visitStatusAdapter.addData(list)
        binding.visitRvstatus.apply {
            adapter = visitStatusAdapter
            layoutManager = LinearLayoutManager(this@VisitorActivity,RecyclerView.HORIZONTAL,false)

        }

        visitStatusAdapter.setOnItemClickListener { adapter, view, position ->
            visitStatusAdapter.data.forEach {
                it.isCheck = false
            }
            visitStatusAdapter.data[position].isCheck = true
            visitStatusAdapter.notifyDataSetChanged()

            if (position == 0){
                viewModel.visit()
            }else{
                viewModel.visStatus(visitStatusAdapter.data[position].status)
            }
        }

        binding.rvVisit.apply {
            adapter = visitAdapter
            layoutManager = LinearLayoutManager(this@VisitorActivity)
        }

        visitAdapter.setOnItemClickListener { adapter, view, position ->
            ARouter.getInstance().build("/app/visitor/VisitApproveActivity")
                .withString("vid",visitAdapter.data[position].id.toString())
                .navigation()
        }

        visitAdapter.setOnItemLongClickListener { adapter, view, position ->
            viewModel.deleteVisit(visitAdapter.data[position].id)
            visitAdapter.data.removeAt(position)
            visitAdapter.notifyItemRemoved(position)
            return@setOnItemLongClickListener true
        }

    }

    override fun initData() {
        viewModel.visitSuccess.observe(this){
            visitAdapter.data.clear()
            visitAdapter.apply {
                addData(it)
                notifyDataSetChanged()
            }
        }
        viewModel.deleteVisitSuccess.observe(this){
            ToastUtils.showLong("删除成功")
        }
    }

    override fun onResume() {
        super.onResume()
        viewModel.visit()
    }

}